<%--
  Created by IntelliJ IDEA.
  User: sunyb
  Date: 2017/8/3
  Time: 13:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/base.jsp" %>
<html>
<head>
    <link href="${ctx}/jquery-easyui-1.5.1/themes/default/easyui.css" rel="stylesheet" type="text/css"/>
    <link href="${ctx}/jquery-easyui-1.5.1/themes/icon.css" rel="stylesheet" type="text/css">
    <link href="${ctx}/jquery-easyui-1.5.1/themes/icon-standard.css" rel="stylesheet" type="text/css">
    <link href="${ctx}/jquery-easyui-1.5.1/themes/icon-hamburg.css" rel="stylesheet" type="text/css">
    <link href="${ctx}/css/head.css" rel="stylesheet" type="text/css">
    <link href="${ctx}/css/index.css" rel="stylesheet" type="text/css">
    <script src="${ctx}/js/jquery.min.js"></script>
    <script src="${ctx}/js/HdUtil.js"></script>
    <script src="${ctx}/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
    <script src="${ctx}/jquery-easyui-1.5.1/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //初始化菜单树
            $("#menuTree").tree({
                method: "get",
                url: "${ctx}/SysResource/getAllMenu",
                onClick: function (node) {
                    $("#menuForm").form("load", node);
                    if (node.disabled == "1") {
                        $("#disabled").prop("checked", true);
                    } else {
                        $("#disabled").prop("checked", false);
                    }
                    $("#permissionTable").datagrid({
                        url: "${ctx}/SysResource/getPermissionByResourceId",
                        queryParams: {
                            resourceId: node.id
                        }
                    });
                }
            });
            //初始化权限datagrid
            $("#permissionTable").datagrid({
                method: "get",
                pagination: true,
                singleSelect: true,
                rownumbers: true,
                pageSize: 20,
                striped: true,
                toolbar: "#resourceTableToolBar",
                fit: true,
                fitColumns: false,
                columns: [[
                    {
                        field: "permissionName",
                        title: "权限名称",
                        multiSort: true,
                        width: 130,
                        halign: "center"
                    },
                    {
                        field: "permissionCode",
                        title: "权限代码",
                        multiSort: true,
                        width: 130,
                        halign: "center"
                    },
                ]]
            });
            $("#permissionDialog").dialog({
                title: "箱类型维护",
                width: 280,
                height: 180,
                closed: true,
                cache: false,
                modal: true,
                buttons: [{
                    text: '保存',
                    handler: function () {
                        savePermission();
                    }
                }, {
                    text: '关闭',
                    handler: function () {
                        $("#permissionDialog").dialog("close");
                    }
                }]
            });
        });

        //添加同级节点
        function addTreeNode() {
            var node = $("#menuTree").tree("getSelected");
            if (node) {
                $("#menuForm").form("clear");
                $("#parentId").val(node.parentId);
            } else {
                HdUtil.messager.error("请选择节点");
            }
        }

        //添加子节点
        function addLeafNode() {
            var node = $("#menuTree").tree("getSelected");
            if (node) {
                $("#menuForm").form("clear");
                $("#parentId").val(node.id);
            } else {
                HdUtil.messager.error("请选择节点");
            }
        }

        //删除节点
        function deleteNode() {
            var node = $("#menuTree").tree("getSelected");
            if (node) {
                $.messager.confirm(HdUtil.constant.CONFIRM, HdUtil.constant.DELETE_CONFIRM, function (r) {
                    if (r) {
                        $.ajax({
                            type: "POST",
                            contentType: "application/x-www-form-urlencoded",
                            url: "${ctx}/SysResource/delete",
                            data: {
                                resourceId: node.id
                            },
                            success: function (data) {
                                if (data == "success") {
                                    HdUtil.messager.slide(HdUtil.constant.DELETE_SUCCESS);
                                    $("#menuForm").form("clear");
                                    $("#menuTree").tree("reload");
                                    $("#permissionTable").datagrid("reload");
                                } else {
                                    HdUtil.messager.error(data);
                                }
                            }
                        })
                    }
                });
            } else {
                HdUtil.messager.error("请选择要删除的菜单");
            }
        }

        //保存树属性
        function saveResource() {
            $("#menuForm").form("submit", {
                url: "${ctx}/SysResource/save",
                onSumbit: function () {
                    $("#menuForm").form.validate();
                },
                success: function (data) {
                    if ("success" == data) {
                        HdUtil.messager.slide(HdUtil.constant.SAVE_SUCCESS);
                        var selectTreeId = $("#menuTree").tree("getSelected").id;
                        $("#menuTree").tree("reload");
                        $("#menuTree").tree({
                            onLoadSuccess: function () {
                                var node = $('#menuTree').tree('find', selectTreeId);
                                $("#menuTree").tree("select", node.target);
                            }
                        });
                    } else {
                        HdUtil.messager.error(data);
                    }
                }
            });
        }

        //保存权限
        function savePermission() {
            $("#permissionForm").form("submit", {
                url: "${ctx}/SysResource/savePermission",
                onSumbit: function () {
                    $("#permissionForm").form.validate();
                },
                success: function (data) {
                    if ("success" == data) {
                        $("#permissionDialog").dialog("close");
                        HdUtil.messager.slide("保存成功");
                        $("#permissionTable").datagrid("reload");
                    } else {
                        HdUtil.messager.error(data);
                    }
                }
            });
        }

        //新增权限
        function addPermission() {
            var node = $("#menuTree").tree("getSelected");
            if (node) {
                $("#permissionForm").form("clear");
                //设置parentId字段值
                var treeSelectNode = $("#menuTree").tree("getSelected");
                $("#permissionParentId").val(treeSelectNode.id);
                $("#permissionDialog").dialog("open");
            } else {
                HdUtil.messager.error("请选择菜单");
            }
        }

        //更新权限
        function updatePermission() {
            var selectRow = $("#permissionTable").datagrid("getSelected");
            if (selectRow) {
                $("#permissionForm").form("clear").form("load", selectRow);
                $("#permissionDialog").dialog("open");
            } else {
                HdUtil.messager.error("请选择要更新的权限");
            }
        }

        //删除权限
        function deletePermission() {
            var selectRow = $("#permissionTable").datagrid("getSelected");
            if (selectRow) {
                $.messager.confirm(HdUtil.constant.CONFIRM, HdUtil.constant.DELETE_CONFIRM, function (r) {
                    if (r) {
                        $.ajax({
                            type: "post",
                            url: "${ctx}/SysResource/delete",
                            data: {
                                resourceId: selectRow.resourceId
                            },
                            success: function (data) {
                                if (data == "success") {
                                    HdUtil.messager.slide("删除成功");
                                    $("#permissionTable").datagrid("reload");
                                } else {
                                    HdUtil.messager.error(data);
                                }
                            }
                        })
                    }
                });
            } else {
                HdUtil.messager.error("请选择要删除的权限");
            }
        }
    </script>
</head>
<body>
<div class="easyui-layout" fit="true">
    <div data-options="region:'west',split:true" style="width: 400px">
        <div>
            <shiro:hasPermission name="	resource:add">
                <a class="easyui-linkbutton" iconCls="icon-add" plain="false" onclick="addTreeNode()">添加同级节点</a>
                <a class="easyui-linkbutton" iconCls="icon-add" plain="false" onclick="addLeafNode()">添加子节点</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="	resource:delete">
                <a class="easyui-linkbutton" iconCls="icon-remove" plain="false" onclick="deleteNode()">删除</a>
            </shiro:hasPermission>

        </div>
        <ul id="menuTree"></ul>
    </div>
    <div data-options="region:'center',split:true">
        <form id="menuForm">
            <table>
                <tr>
                    <td>菜单名：</td>
                    <td>
                        <input type="hidden" id="id" name="id"/>
                        <input type="hidden" id="parentId" name="parentId"/>
                        <input class="easyui-textbox" id="menuName" name="menuName" data-options="required:true"/>
                    </td>
                </tr>
                <tr>
                    <td>路径：</td>
                    <td>
                        <input class="easyui-textbox" id="url" name="url"/>
                    </td>
                </tr>
                <tr>
                    <td>排序：</td>
                    <td>
                        <input class="easyui-numberbox" id="seqNum" name="seqNum"/>
                    </td>
                </tr>
                <tr>
                    <td>图标：</td>
                    <td>
                        <input class="easyui-textbox" id="icon" name="icon" type="text"/>
                    </td>
                </tr>
                <tr>
                    <td>禁用：</td>
                    <td>
                        <input id="disabled" name="disabled" type="checkbox"/>
                    </td>
                </tr>
                <tr>
                    <td>描述：</td>
                    <td>
                        <input class="easyui-textbox" id="remark" name="remark"/>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <a class="easyui-linkbutton" iconCls="icon-save" plain="false" onclick="saveResource()">保存</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div data-options="region:'south',split:true" style="height: 300px">
        <div id="resourceTableToolBar">
            <shiro:hasPermission name="	resource:add">
                <a class="easyui-linkbutton" iconCls="icon-add" onclick="addPermission()">增加</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="	resource:edit">
                <a class="easyui-linkbutton" iconCls="icon-edit" onclick="updatePermission()">修改</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="	resource:delete">
                <a class="easyui-linkbutton" iconCls="icon-remove" onclick="deletePermission()">删除</a>
            </shiro:hasPermission>
        </div>
        <table id="permissionTable"></table>
    </div>
</div>
<div id="permissionDialog">
    <form id="permissionForm" action="${ctx}/SysResource/savePermission">
        <table>
            <tr>
                <td>权限名称：</td>
                <td>
                    <input type="hidden" name="resourceId"/>
                    <input type="hidden" id="permissionParentId" name="parentId"/>
                    <input name="permissionName" class="easyui-validatebox" data-options="required:true"/>
                </td>
            </tr>
            <tr>
                <td>权限代码：</td>
                <td>
                    <input name="permissionCode" class="easyui-validatebox" data-options="required:true"/>
                </td>
            </tr>
        </table>
    </form>

</div>
</body>
</html>
